//
//  DefineNavView.m
//  KeleAppProject
//
//  Created by Cen Zhou on 2018/1/13.
//  Copyright © 2018年 Cen Zhou. All rights reserved.
//

#import "DefineNavView.h"


@interface DefineNavView()
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) NSMutableArray *buttonsArray;
// 记录上一次的button的位置
@property (nonatomic, assign) NSInteger  indexFlag;
@property (nonatomic, strong) UIView *trackView;
// 记录上一次的track的位置
@property (nonatomic, assign) NSInteger indexTrack;
// 记录传进来的 width  和  distance
@property (nonatomic, assign) CGFloat w;
@property (nonatomic, assign) CGFloat d;
@property (nonatomic, assign) CGFloat wd; // 记录和

@end

@implementation DefineNavView

- (instancetype)initWithFrame:(CGRect)frame
                   titleArray:(NSArray *)titleArray
                     distance:(CGFloat)distance
                        width:(CGFloat)width{
    if (self = [super initWithFrame:frame]) {
        self.titleArray = titleArray;
        self.w = width;
        self.d = distance;
        self.wd = width + distance;
        [self loadSubviewsOnDefineNavWithArr:titleArray
                                    distance:distance
                                       width:width];
        
        
  

        
    }
    return self;
}

- (void)addNotificationWithName:(NSString *)notificatioName{
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:notificatioName object:nil] subscribeNext:^(NSNotification *x) {
        //  改变导航栏状态
        NSInteger index = [x.object[@"index"] integerValue];
        [self allAnimationWithButton:self.buttonsArray[index]];
    }];
}


//
- (void)loadSubviewsOnDefineNavWithArr:(NSArray *)arr
                              distance:(CGFloat)distance
                                 width:(CGFloat)width{
    
    UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height)];
    contentView.backgroundColor = [UIColor whiteColor];
    [self addSubview:contentView];
    for (NSInteger i = 0; i < arr.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(i*(width +distance)*WIDTH, 13, width*WIDTH, 22);
        [button setTitle:arr[i] forState:UIControlStateNormal];
        button.tag = 100 + i;
        if (i == 0) {
            [button setTitleColor:KLColor_A1 forState:UIControlStateNormal];
        }else{
            [button setTitleColor:KLColor_B6 forState:UIControlStateNormal];
        }
        button.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16*WIDTH];
        [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(UIButton *x) {
            [self allAnimationWithButton:x];
            if (self.delegateSignal) {
                [self.delegateSignal sendNext:@{@"index":@(x.tag - 100)}];
            }
            NSLog(@"点我点我");
        }];
        [contentView addSubview:button];
        [self.buttonsArray addObject:button];
    }
    
    // 追踪器
    UIView *trackView = [[UIView alloc]init];
    trackView.backgroundColor = KLColor_A1;
    trackView.frame = CGRectMake(6, 35, 20*WIDTH, 2);
    trackView.layer.masksToBounds = YES;
    trackView.layer.cornerRadius = 1;
    self.indexTrack = 0;
    [contentView addSubview:trackView];
    self.trackView = trackView;
    
    
}

// 所有的动画整合
- (void)allAnimationWithButton:(UIButton *)sender{
    NSInteger index = sender.tag - 100;
    [self addAnimationWithItems:index];
    [self filterButtonChange];
    sender.selected = YES;
    [sender setTitleColor:KLColor_A1 forState:UIControlStateNormal];
    // 追踪器改变frame
    [self trackAnimationWithButton:index];
    // 记录追踪器的位置
    self.indexTrack = index;
}



// 追踪器动画
- (void)trackAnimationWithButton:(NSInteger )index{
//    NSInteger index = btn.tag - 100;
    if (index > self.indexTrack) { // 向左
        [UIView animateWithDuration:0.2 animations:^{
            self.trackView.frame = CGRectMake((6+self.wd * (index - 1))*WIDTH, 35, (self.wd+20)*WIDTH, 2);
        } completion:^(BOOL finished) {
            self.trackView.frame = CGRectMake((6+self.wd * index)*WIDTH, 35, 20*WIDTH, 2);
        }];
    }else if(index < self.indexTrack){  // 向右
        [UIView animateWithDuration:0.2 animations:^{
            self.trackView.frame = CGRectMake((6+self.wd * index)*WIDTH, 35, (self.wd+20)*WIDTH, 2);
        } completion:^(BOOL finished) {
            self.trackView.frame = CGRectMake((6+self.wd * index)*WIDTH, 35, 20*WIDTH, 2);
        }];
    }
}

// 动画
- (void)addAnimationWithItems:(NSInteger)index{
//    NSInteger index = [self.buttonsArray indexOfObject:item];
    if (index != self.indexFlag) {
        //执行动画
        NSMutableArray *arry = [NSMutableArray array];
        for (UIView *btn in self.buttonsArray) {
            if ([btn isKindOfClass:NSClassFromString(@"UIButton")]) {
                [arry addObject:btn];
            }
        }
        //添加动画
        //放大效果，并回到原位
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
        //速度控制函数，控制动画运行的节奏
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        animation.duration = 0.1;       //执行时间
        animation.repeatCount = 1;      //执行次数
//        animation.autoreverses = YES;    //完成动画后会回到执行动画之前的状态
        animation.fromValue = [NSNumber numberWithFloat:0.7];   //初始伸缩倍数
        animation.toValue = [NSNumber numberWithFloat:1.3];     //结束伸缩倍数
        [[arry[index] layer] addAnimation:animation forKey:nil];
        self.indexFlag = index;
    }
}

// 过滤
- (void)filterButtonChange{
    [self.buttonsArray enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.selected = NO;
        [obj setTitleColor:KLColor_B6 forState:UIControlStateNormal];
    }];
    
}


//
- (NSArray *)titleArray{
    if (!_titleArray) {
        _titleArray = [NSArray array];
    }
    return _titleArray;
}

- (NSMutableArray *)buttonsArray{
    if (!_buttonsArray) {
        _buttonsArray = [NSMutableArray array];
    }
    return _buttonsArray;
}


@end
